import java.util.*;
public class StatusCheck {

	/**
	 * @param args
	 */
	private Request _currentRequest;
	public Queue<String> q;
	public StatusCheck(){
		_currentRequest = new Request(new Date(System.currentTimeMillis()), null, null);
		_currentRequest.setEntriesExpected(21);
		_currentRequest.setBit(1, "peer1");
		_currentRequest.setBit(5, "peer5");
		_currentRequest.setBit(6, "peer6");
		_currentRequest.setBit(9, "peer9");
		_currentRequest.setBit(20, "peer20");
		
		q = new LinkedList<String>();
	}
	
	public static void main(String[] args) {
		StatusCheck s = new StatusCheck();
		s.generateSubRequests();
		System.out.println(s.q.toString());

	}
	public void generateSubRequests(){
		int i = 1;
		int limit = _currentRequest.getEntriesExpected();
		String left = null;
		String right = null;
		//int indexLeft, 
		int indexRight;
		//indexLeft = 0;
		indexRight = 1;
		
		while (i < limit) {
			String data = null;
			String sEmpty = "-1";
			if (!_currentRequest.wasArrived(i).equals(sEmpty) ) {
				left = _currentRequest.wasArrived(i);
				//indexLeft = i;
				++i;
			} else {
				//right = left; //Check last index
				int j = i;
				right = null;
				
				//while (j < limit && right != null)
				
				while (j < limit && right == null) {
					if (!_currentRequest.wasArrived(j).equals(sEmpty)) {
						right = _currentRequest.wasArrived(j);
						indexRight = j;
					}
					else 
						++j;
				}
			
				if (right == null) { // now - ALL {
					data = String.format("REQUESTENTRY<%s,%s,ALL>", Utils.DateToString(_currentRequest.getDbVersion()),left);
					i = limit; // i = j;
					// create new request
					q.add(data);
				} else { //left - right
					if (left == null) {
						//SEND with 0 param
						data = String.format("REQUESTENTRY<%s,0,%s>", Utils.DateToString(_currentRequest.getDbVersion()),right);
					} else {
						//send left - right
						data = String.format("REQUESTENTRY<%s,%s-%s>", Utils.DateToString(_currentRequest.getDbVersion()), left, right);
					}
					// create new request
					q.add(data);
					i = indexRight;
					//indexLeft = indexRight;
				}
			}
			
		}
	}

}
